package com.itestor.iptv.modules.common.actions;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.itestor.iptv.global.utils.LoggerUtils;
import com.itestor.iptv.modules.common.domain.RoleGroup;
import com.itestor.iptv.modules.common.service.UserService;
import com.itestor.iptv.modules.common.utils.CommonConstant;
import com.itestor.iptv.modules.common.vo.User;

/**
 * 
 * @author Mr.li
 * 
 */
public class UserNaviAction extends DispatchAction {

    /**
     * Logger for this class.
     */
    private static final Logger logger = Logger.getLogger(UserNaviAction.class);

    private UserService userService;

    /**
     * 添加系统用户
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward add(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        request.setAttribute(CommonConstant.REQUEST_ATTR_ROLES, userService
                .getRoleMap());
        
        return mapping.findForward(CommonConstant.FORWARD_MANAGER_ADD);

    }

    /**
     * 修改系统用户
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward modify(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String username = request.getParameter("username");
        if (StringUtils.isEmpty(username)) {
            return null;
        }
        User user = userService.get(username);
        if (user == null) {
            return null;
        }

        LoggerUtils.logActionInfo("To modify the user " + username + ".",
                logger, request);

        request.setAttribute(CommonConstant.REQUEST_ATTR_USER, user);
        request.setAttribute(CommonConstant.REQUEST_ATTR_ROLES, userService
                .getRoleMap());

        return mapping.findForward(CommonConstant.FORWARD_MANAGER_MODIFY);
    }
    
    /**
     * 修改系统用户
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward grant(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String username = request.getParameter("username");
        if (StringUtils.isEmpty(username)) {
            return null;
        }
        User user = userService.get(username);
        if (user == null) {
            return null;
        }
        
        LoggerUtils.logActionInfo("To modify the user " + username + ".",
                logger, request);
        
        List<RoleGroup> roleGroupList=userService.findAllRoleGroup();        
        request.setAttribute("roleGroupList", roleGroupList);     
        
        
        request.setAttribute(CommonConstant.REQUEST_ATTR_USER, user);
        request.setAttribute(CommonConstant.REQUEST_ATTR_ROLES, userService
                .getRoleMap());
     
        return mapping.findForward(CommonConstant.FORWARD_MANAGER_GRANT);
    }
    

    /**
     * 删除系统用户
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward delete(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        String[] pvids = request.getParameterValues("id");
        if (null == pvids) {
            String pvid = request.getParameter("id");
            if (!StringUtils.isEmpty(pvid)) {
                pvids = new String[1];
                pvids[0] = pvid;
            }
        }
        if (null != pvids && pvids.length > 0) {
            
            int[] ids = new int[pvids.length];
            for (int i = 0; i < pvids.length; i++) {
                try {
                    ids[i] = Integer.parseInt(pvids[i]);
                } catch (NumberFormatException e) {
                    ids[i] = 0;
                }
            }
            
            LoggerUtils.logActionInfo("Delete the user - id: "
                    + StringUtils.join(pvids, ",") + ".", logger, request);

            userService.delete(ids);
        }

        return mapping.findForward(CommonConstant.FORWARD_MANAGER_DELETE);
    }

    /**
     * @param userService
     */
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

}
